<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="openDialog(1,'0')">添加权限</el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table :data="list" default-expand-all row-key="id" border>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="type" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="openDialog(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermisson(row)">编辑</el-button>
              <el-button type="text" @click="delPermisson(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog :visible.sync="showDialog" title="弹层标题" @close="close">
      <el-form ref="fm" :model="formData" :rules="rules" label-width="100px">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="addPermission()">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission.js'
import { tranListToTreeData } from '@/utils/index.js'
export default {
  data() {
    return {
      // 弹窗数据绑定
      formData: {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, min: 2, max: 10, message: '必填，2-10位', trigger: ['blur', 'change'] }]
      },
      // 添加弹窗显示控制
      showDialog: false,
      // 页面列表
      list: []
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 编辑
    async editPermisson(row) {
      const res = await getPermissionDetail(row.id)
      this.formData = res
      this.showDialog = true
    },
    // 删除
    delPermisson(row) {
      // 询问敏感操作
      this.$confirm('确定删除吗？', '提示').then(async() => {
        if (row.children && row.children.length > 0) {
          this.$message.error('不能直接删除父节点')
        } else {
          // 调用接口
          await delPermission(row.id)
          this.$message.success('操作成功！')
          // 重新获取数据
          this.getList()
        }
      }).catch(console.log)
    },
    // 提交新增表单
    addPermission() {
      // 整体兜底校验
      this.$refs.fm.validate(async(isok) => {
        if (isok) {
          if (this.formData.id) {
            // 编辑提交
            await updatePermission(this.formData)
          } else {
            await addPermission(this.formData)
          }
          this.$message.success('操作成功！')
          // 重新获取数据
          this.getList()
          this.close()
        }
      })
    },
    // 打开弹窗
    openDialog(type, pid) {
      this.showDialog = true
      // ***注意，这里需要判断是给页面加，还是给页面的功能加，两个地方调用该方法，传的参数不一样
      // 给页面加，type是1，pid是0；给页面功能加，type是2，pid是当前行的id
      this.formData.type = type
      this.formData.pid = pid
    },
    async getList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res)
      console.table(res)
    },
    // 关闭弹窗
    close() {
      this.showDialog = false
      this.$refs.fm.resetFields()
      this.formData = {
        enVisible: '1', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
